/**
 * Copyright © 2016 公司名. All rights reserved.
 * 
 * @Title: ProductServiceImpl.java
 * @Prject: LianniuWeb
 * @Package: com.lianniu.web.buyerProduct.service.impl
 * @author: lzz
 * @date: 2016年8月31日 上午10:21:41
 * @version: V1.0
 */
package com.lianniu.web.buyerProduct.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.buyerProduct.dao.ICateDao;
import com.lianniu.web.buyerProduct.dao.ISkuProductDao;
import com.lianniu.web.buyerProduct.service.IProductService;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.product.model.Page;
import com.lianniu.web.product.model.ProductCateVo;
import com.lianniu.web.product.model.SkuProductVo;

/**
 * @ClassName: ProductServiceImpl
 * @author: lzz
 * @date: 2016年8月31日 上午10:21:41
 */
@Service
public class ProductServiceImpl implements IProductService {

    static final Logger LOGGER = Logger.getLogger(ProductServiceImpl.class);

    @Autowired
    private ISkuProductDao skuProductDao;
    @Autowired
    private ICateDao cateDao;
    /*
     * (non Javadoc)
     * 
     * @Title: getList
     * 
     * @Description: 买家商品列表
     * 
     * @author: lzz
     * 
     * @param userId
     * 
     * @param pageNum
     * 
     * @return
     * 
     * @see com.lianniu.web.buyerProduct.service.IProductService#getList(java.lang.String,
     * java.lang.Integer)
     * 
     * @date: 2016年8月31日 上午10:41:16
     */
    @Override
    public BaseResult getList(String userId, Integer pageNum,String cateId,String searchName,boolean iszTrend) {
        LOGGER.info("ProductServiceImpl getList begin");
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("cateId", cateId);
        param.put("name",searchName);
        param.put("isTrend","is"+String.valueOf(iszTrend));
        int b = skuProductDao.checkIsSupply(userId);
        if(b>0){
        	param.put("supply", "sypply");
        }
        int count = 0;
        Page page =null;
        
       try {
           count = skuProductDao.selectMyProductCount(param);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }
        if(StringUtils.isBlank(userId) || "null".equals(userId)){
            count = 3;
        }
        page = new Page(count, pageNum);
        param.put("page", page);
        
        List<SkuProductVo> list = null;
        
        try {
            if (pageNum <= page.getPageSum()) {
                list = skuProductDao.getList(param);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }
        
        LOGGER.info("ProductServiceImpl getList end");
        return BaseResult.success(list);
    }

    @Override
    public BaseResult getTrendList(String userId, Integer pageNum, String cateId, String searchName) {
        LOGGER.info("ProductServiceImpl getList begin");
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        param.put("cateId", cateId);
        param.put("name",searchName);
        int b = skuProductDao.checkIsSupply(userId);
        if(b>0){
            param.put("supply", "sypply");
        }
        int count = 0;
        Page page =null;

        try {
            count = skuProductDao.getTrendListCount(param);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }
        if(StringUtils.isBlank(userId) || "null".equals(userId)){
            count = 3;
        }
        page = new Page(count, pageNum);
        param.put("page", page);

        List<SkuProductVo> list = null;

        try {
            if (pageNum <= page.getPageSum()) {
                list = skuProductDao.getTrendList(param);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }

        LOGGER.info("ProductServiceImpl getList end");
        return BaseResult.success(list);
    }

    /*
     * (non Javadoc)
     * 
     * @Title: getProDetail
     * 
     * @Description: 商品详情
     * 
     * @author: lzz
     * 
     * @param id
     * 
     * @return
     * 
     * @see com.lianniu.web.buyerProduct.service.IProductService#getProDetail(java.lang.String)
     * 
     * @date: 2016年8月31日 上午10:40:56
     */
    @Override
    public BaseResult getProDetail(String id) {
        LOGGER.info("ProductServiceImpl getProDetail begin");
        SkuProductVo skuProductVo = null;
        try {
            skuProductVo = skuProductDao.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }
        LOGGER.info("ProductServiceImpl getProDetail end");
        return BaseResult.success(skuProductVo);
    }

    @Override
    public BaseResult loadCate(String cateId) {
        
        LOGGER.info("ProductServiceImpl loadCate begin");
        List<ProductCateVo> list = null;
        try {
            list = cateDao.loadCate(cateId);
        } catch (Exception e) {
            LOGGER.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }
        LOGGER.info("ProductServiceImpl loadCate end");
        
        return BaseResult.success(list);
    }
    @Override
    public BaseResult loadSecondCate(String cateId) {

        LOGGER.info("ProductServiceImpl loadCate begin");
        List<ProductCateVo> list = null;
        try {
            list = cateDao.loadSecondCate(cateId);
        } catch (Exception e) {
            LOGGER.info(ErrorCode.SQLExceptionRetInfo);
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }
        LOGGER.info("ProductServiceImpl loadCate end");

        return BaseResult.success(list);
    }

    @Override
    public BaseResult selectByProductName(String userId, Integer pageNum,String name,String cateId) {
      LOGGER.info("selectByProductName begin");
      LOGGER.info("datacheck "+name);
      Map<String, Object> map = new HashMap<>();
      map.put("userId", userId);
      map.put("name", name);
      map.put("cateId", cateId);
      int b = skuProductDao.checkIsSupply(userId);
      if(b>0){
    	  map.put("supply", "sypply");
      }
      int count = 0;
      Page page =null;
      try{
          count = skuProductDao.selectMyProductCount(map); 
      }catch(Exception e){
          e.printStackTrace();
          LOGGER.info(ErrorCode.SQLExceptionRetInfo);
          return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
      }
      page = new Page(count, pageNum);
      map.put("page", page);
      
      List<SkuProductVo> list = null;
      try{
          if (pageNum <= page.getPageSum()) {
              list = skuProductDao.selectByProductName(map);
          }
      }catch(Exception e){
          e.printStackTrace();
          LOGGER.info(ErrorCode.SQLExceptionRetInfo);
          return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
      }
        return BaseResult.success(list);
    }



}
